#include<iostream>
#include<cstring>
using namespace std;

const int N=20;
int arr[N], brr[N], n;
int fun1(int x, int y)
{
	int ret=0;
	for(int i=0; i<n; i++)
	{
        if(((x >> i) & 1) == 0 && ((y >> i) & 1) == 1) ret++;
        if(((x >> i) & 1) == 1 && ((y >> i) & 1) == 0) return -1; 
	}
	return ret;
}
int fun()
{
	int ret=0x3f3f3f3f;
	for(int i=0; i<(1<<n); i++)
	{
		memcpy(brr, arr, sizeof arr);
		int push=i, sign=1, num=0;
		for(int j=1; j<=n; j++)
		{
			int _num=fun1(brr[j], push);
			if(_num==-1) 
			{
				sign=0;
				break;
			}
			num+=_num;
			brr[j]=push;
			push=brr[j-1]^(brr[j]<<1)^(brr[j]>>1);
			push&=(1<<n)-1;
		}
		if(sign) 
		{
			ret=min(ret, num);
		}
	}
	if(ret==0x3f3f3f3f) return -1;
	else return ret;
}
int main()
{
	int T; cin>>T;
	for(int k=1; k<=T; k++)
	{
		memset(arr, 0, sizeof arr);
		cin>>n;
		for(int i=1; i<=n; i++)
		{
			for(int j=0; j<n; j++)
			{
				int ch; cin>>ch;
				if(ch==1) arr[i]=arr[i]|(1<<j);
			}
		}
		
		printf("Case %d: %d\n", k, fun());
		
	}
	return 0;
}
